package com.ieslab.powergrid.demosvr.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ieslab.powergrid.demosvr.entity.Person;
import com.ieslab.powergrid.demosvr.mapper.PersonMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ThreadServiceDemo {
        @Autowired
        private PersonMapper personMapper;
        public static boolean runFlag = false;

        // 这里进行标注为异步任务，在执行此方法的时候，会单独开启线程来执行(并指定线程池的名字)
        @Async("taskExecutor")
        public void findPersonTest() throws InterruptedException {
            while (runFlag){
                log.info("访问数据库开始");
                IPage<Person> page = new Page<Person>(0,5);
                IPage<Map<String, Object>> pageData = personMapper.selectMapsPage(page,null);
                List list = pageData.getRecords();
                for (Object obj:list){
                    log.info(obj.toString());
                }
                log.info("访问数据库结束");

                //线程等待，在接收数据时，经常使用，此处我们就直接访问数据进行测试
                try {
                    Thread.sleep(5*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }

